<?php

include('lib/file/class.upload.php');

if(isset($_POST['cmd']) && $_POST['cmd'] == 'upload') {
	
	// usage: Upload(max_size,max_width,max_height,thi_file);
	// note: with max_size = 0, disables maximum size restriction
	// same with the params max_width and max_height
	$upload = new Upload(100000,130,130,$_FILES['file']);
	
	// the user (you) just have to think to upload the file
	// and the class deals with all of the verification if its a valid image with the right sizes ;)
	// remember that upload() function returns the result of the operation if its an error
	// you can fetch the error msgs, wich is an array, by doing: $upload->getErrors()
	$dir = "uploads"; 													// the directory to upload the files
	$name = md5(uniqid(time())) . "." . $upload->getExtension(); 		// the name desired for the file, a nice random ;)
	
	// make sure the dir mentioned is writable -> chmod 0777 dir
	
	$path = $dir . "/" . $name; // the function upload() just needs to know the path to where to save the file
	
	// let's say we want to resize the image of its over the maximums allowed
	// usage: resizeImage(new_width,new_height,keep_aspect_ratio?);
	$upload->resizeImage(128,128,true);
	
	if($upload->uploadImage($path)) {
		echo "Upload sucessfull!<br />";
		// echo the image
		echo "<img src=\"".$path."\" alt=\"The Image\" border=\"0\" width=\"".$upload->getWidth()."\" height=\"".$upload->getHeight()."\" />";
	} else {
		$errors = $upload->getErrors();
		
		foreach($errors as $error) {
			echo $error . "<br /><br />";
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test page</title>
</head>

<body>
<form action="../index.php?method=uploadPhoto" method="post" enctype="multipart/form-data" name="form1" id="form1">
  <label for="file">Upload</label>
  <p>
    <input type="file" name="file" id="file" />
</p>
  <p>
  	<input name="cmd" id="cmd" type="hidden" value="upload" />
    <input type="submit" name="Submit" value="Submit" id="Submit" />
</p>
</form>
</body>

</html>
